<?php declare(strict_types=1);


namespace PinWuYun\domain\service\youZanServer;

use Exception;
use PinWuYun\domain\data\YouZanData\GoodsData;
use Youzan\Open\Client;

/**
 * Created: by zhiJie
 * Date: 2021/6/23
 */
class GoodsServer
{
    protected $GoodsData;

    public function __construct()
    {
        $this->GoodsData = new  GoodsData();
    }

    /**
     * 商品添加操作
     * @param array $spuDetail spu详情，包括sku、价格、库存
     * @param string $accessToken
     * @return mixed
     * @throws Exception
     */
    public function addGoods(array $spuDetail, string $accessToken)
    {
        $client = new Client(TokenServer::getAccessToken());
        $method = 'youzan.item.add';
        $apiVersion = '1.0.0';
        /** 数据格式处理 */
        $requestParam = $this->GoodsData->buildAddItem($spuDetail, $accessToken);
        return $client->post($method, $apiVersion, ['param' => $requestParam]);
    }

    /**
     * 上架
     * @param array $paramPre
     * @param string $accessToken
     * @return mixed
     * @throws Exception
     */
    public function putOnTHeShelf(array $paramPre, string $accessToken)
    {
        $client = new Client(TokenServer::getAccessToken());
        $method = 'youzan.item.add';
        $apiVersion = '1.0.0';
        $params = [
            'item_id' => $paramPre['item_id'],
            // 用户id
            'yz_open_id' => $paramPre['yz_open_id']
        ];
        return $client->post($method, $apiVersion,$params);
    }

    /**
     * 下架
     * @param array $paramPre
     * @param string $accessToken
     * @return mixed
     * @throws Exception
     */
    public function offTHeShelf(array $paramPre, string $accessToken)
    {
        $client = new Client(TokenServer::getAccessToken());
        $method = 'youzan.item.update.listing';
        $apiVersion = '3.0.1';
        $params = [
            'item_id' => $paramPre['item_id'],
            // 用户id
            'yz_open_id' => $paramPre['yz_open_id']
        ];
        return $client->post($method, $apiVersion,$params);
    }

    /**
     * 更新主要数据
     */
    public function update(array $paramPre, string $accessToken)
    {
        $client = new Client($accessToken);
        $method = 'youzan.item.update';
        $apiVersion = '3.0.1';
        //设置参数
        return $client->post($method, $apiVersion, $paramPre);
    }

    /**
     *  更新sku数据
     * @param array $paramPre
     * @param string $accessToken
     * @return mixed
     */
    public function updateSku(array $paramPre, string $accessToken)
    {
        $client = new Client($accessToken);
        $method = 'youzan.item.sku.update';
        $apiVersion = '3.0.0';
        return $client->post($method, $apiVersion, $paramPre);
    }

    /**
     * 获取商品详情
     * @throws Exception
     */
    public function getGoodsDetail($alias)
    {
        $accessToken = TokenServer::getAccessToken();
        $client = new Client($accessToken);

        $method = 'youzan.item.detail.api.Item.alias';
        $apiVersion = '1.0.0';
        return $client->post($method, $apiVersion, ["alias"=> $alias]);

    }

    /**
     * 获取有赞模板列表
     * @throws Exception
     */
    public function getTempLateList()
    {
        $accessToken = TokenServer::getAccessToken();
        $client = new Client($accessToken);

        $method = 'youzan.item.template.list.search';
        $apiVersion = '3.0.0';
        //设置参数
        $params =[
            "page_no"=>"1",
            "operator"=>"",
            "page_size"=>"1000"
        ];
        return $client->post($method, $apiVersion, $params);
    }
}